package com.mzy;

public class QueueLinked {

    public static class Node {
        int value;
        Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    public static class MyQueue {
        Node head;
        Node tail;
        int size;

        public MyQueue() {
            head = tail = null;
            size = 0;
        }

        public int size() {
            return this.size;
        }

        public boolean isEmpty() {
            return this.size == 0;
        }

        public void offer(int value) {

            Node cur = new Node(value);
            // 判断是否是首次添加
            if (head == null && tail == null) {
                head = cur;
                tail = cur;
            } else {
                tail.next = cur;
                tail = cur;
            }
            size++;
        }

        public int poll() {
            if (size == 0) {
                return -1;
            }
            int ans = 0;
            if (head != null) {
                ans = head.value;
                head = head.next;
                size--;
            }
            if (head == null) {
                tail = null;
            }
            return ans;
        }

        public int peek() {
            int ans = 0;
            if (size == 0) {
                return -1;
            }

            if (head != null) {
                ans = head.value;
            }
            return ans;
        }


    }
}
